﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<WebSite.Models.RoleRelationModel>" %>
<%@ Import Namespace="WebSite.Models" %>
<%@ Import Namespace="WebSite.App_Code" %>
<%@ Import Namespace="WebSite.App_Data" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Department
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
    

    <script type="text/javascript">

        $(document).ready(function () {

            if ('<%=ViewData.ContainsKey("DB.Operation.Result") %>' == "True")
                alert('<%=ViewData["DB.Operation.Result"] %>');

            $("#divUpdate").dialog({
                autoOpen: false,
                height: 325,
                width: 400,
                modal: true
            });

        });

        function EditRoleRelation(sender) {

            var all = $("#ddlRoleAll > option");
            var old_ele = $("<select></select>")

            old_ele.append($("select", $(sender).parents("tr:eq(0)")).clone());
            
            $("#ddlRoleNoSelect").empty();
            $("#ddlRoleSelect").empty();

            for (var i = 0; i < all.length; i++) {

                var isOld = ($("option[value=" + all.eq(i).val() + "]", old_ele).length > 0);

                if (isOld)
                    $("#ddlRoleSelect").append("<option value=\"" + all.eq(i).val() + "\">" + all.eq(i).text() + "</option>");
                else
                    $("#ddlRoleNoSelect").append("<option value=\"" + all.eq(i).val() + "\">" + all.eq(i).text() + "</option>");
            }

            $("#UpdateUser").val($("input[type=hidden]:eq(0)", $(sender).parents("tr:eq(0)")).val());

            $("#divUpdate").dialog("open");
        }

        function AddRole() {
            var all = $("#ddlRoleAll > option");
            var old_ele = $("<select></select>")
            var new_ele = $("<select></select>")

            old_ele.append($("#ddlRoleSelect > option"));
            new_ele.append($("#ddlRoleNoSelect > option:selected"));
            
            $("#ddlRoleNoSelect").empty();
            $("#ddlRoleSelect").empty();

            for (var i = 0; i < all.length; i++) {

                var isOld = ($("option[value=" + all.eq(i).val() + "]", old_ele).length > 0);
                var isNew = ($("option[value=" + all.eq(i).val() + "]", new_ele).length > 0);

                if (isOld || isNew)
                    $("#ddlRoleSelect").append("<option value=\"" + all.eq(i).val() + "\">" + all.eq(i).text() + "</option>");
                else
                    $("#ddlRoleNoSelect").append("<option value=\"" + all.eq(i).val() + "\">" + all.eq(i).text() + "</option>");
            }

        }

        function RemoveRole() {
            var all = $("#ddlRoleAll > option");
            var old_ele = $("<select></select>")
            var new_ele = $("<select></select>")

            new_ele.append($("#ddlRoleSelect > option:selected"));
            old_ele.append($("#ddlRoleSelect > option"));

            $("#ddlRoleNoSelect").empty();
            $("#ddlRoleSelect").empty();

            for (var i = 0; i < all.length; i++) {

                var isOld = ($("option[value=" + all.eq(i).val() + "]", old_ele).length > 0);

                if (isOld)
                    $("#ddlRoleSelect").append("<option value=\"" + all.eq(i).val() + "\">" + all.eq(i).text() + "</option>");
                else
                    $("#ddlRoleNoSelect").append("<option value=\"" + all.eq(i).val() + "\">" + all.eq(i).text() + "</option>");
            }

        }

        function SaveData() {

            var sel = $("#ddlRoleSelect > option")
            var context = "";

            for (var i = 0; i < sel.length; i++) {
                context = context + sel.eq(i).val() + ",";
            }

            $("#UpdateRole").val(context);
        }

    </script>

    
    <div id="divUpdate">
        <% Html.BeginForm("RR_Update", "MasterData", FormMethod.Post); %>
        <%=Html.DropDownList("RoleAll", Model.RoleSource, new { id="ddlRoleAll", style="display:none" }) %>
        <%=Html.Hidden("UpdateRole") %>
        <%=Html.Hidden("UpdateUser") %>
        <center>
        <br />
        <table>
            <tr>
                <td rowspan="2"><select name="RoleNoSelect" id="ddlRoleNoSelect" multiple="multiple" style="width:125px; height:200px"></select></td>
                <td style=" padding:40px 20px 0px 20px"><input value=" ＞ " type="button" onclick="AddRole()" /></td>
                <td rowspan="2"><select name="RoleSelect" id="ddlRoleSelect" multiple="multiple" style="width:125px; height:200px"></select></td>
            </tr>
            <tr>
                <td style=" padding:0px 20px 20px 20px "><a id="Show"></a><input value=" ＜ " type="button" onclick="RemoveRole()" /></td>
            </tr>
            <tr>
                <td colspan="3" style=" text-align:center">
                    <br />
                    <input type="submit" value="   保  存   " onclick="SaveData()" />
                </td>
            </tr>
        </table>
        </center>
        <% Html.EndForm(); %>
    </div>
    

    <table border="0" cellpadding="0" cellspacing="1" class="masterdatatable">
        <tr class="masterdatatable_header">
            <td>姓名</td>
            <td>组织</td>
            <td>权限</td>
            <td></td>
        </tr>
            
    <% for (int i = 0; i < Model.Table.Count; i++) { %>
        <% if ((i % 2) == 1) { %> 
            <tr class="masterdatatable_odd" RowType="Display"> 
        <% } else { %> 
            <tr class="masterdatatable_even" RowType="Display">  
        <% } %>
            <td>
                <%=Model.Table[i].User.User.UserName %>
                <%=Html.Hidden("UserID", Model.Table[i].User.User.Guid) %>
            </td>
            <td><%=Model.Table[i].User.Department.Department.DepartmentName %></td>
            <td>
                <% if (Model.Table[i].RoleList.Count == 0) { %>
                    <select><option>N/A</option></select>
                <% } else { %>
                    <%=Html.DropDownList("Role",Model.Table[i].RoleList.AsEnumerable()) %>
                <% } %>
            </td>
            <td>
                <a class="linkbutton" onclick="EditRoleRelation(this)">Edit</a>
            </td>
        </tr> 
    <% } %>         
    </table>
</asp:Content>
